#ifndef PARSER_H
#define PARSER_H
#include"forwards.h"
#include<string>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<list>
#include<map>
#define BUFFER_SIZE 12
using namespace std;
namespace Json {
typedef struct JSON_API Extension
{
	Extension(){}
	Extension(const Extension& other)
	{
		content=other.content;
	}
	map<string,string> content;
	string getElementByName(const string& elem)			//possible element:name,url,version
	{
			return content[elem];
	}
	string getName()
	{
		return content["name"];
	}
	string getVersion()
	{
		return content["version"];
	}
	string getUrl()
	{
		return content["url"];
	}
	string getDescription()
	{
		return content["description"];
	}
	void clear()
	{
		content.clear();
	}
} extension;
typedef struct JSON_API App
{
	App()
	{
		extensionNum=0;
	}
	App(const App &other)
	{
		extensionNum=other.extensionNum;
		content=other.content;
		extensions=other.extensions;
	}
	map<string,string> content;
	string getElementByName(const string& elem)
	{
			return content[elem];
	}
	string getName()
	{
		return content["name"];
	}
	string getIcon()
	{
		return content["icon"];
	}
	string getVstamp()
	{
		return content["vstamp"];
	}
	string getCode()
	{
		return content["code"];
	}
	string getUrl()
	{
		return content["url"];
	}
	int getAid()
	{
		char str[BUFFER_SIZE];
		strcpy(str,content["aid"].c_str());
		int aid=atoi(str);
		return aid;
	}
	int getWidth()
	{
		char str[BUFFER_SIZE];
		strcpy(str,content["width"].c_str());
		int w=(int)atoi(str);
		return w;
	}
	int getHeight()
	{
		char str[BUFFER_SIZE];
		strcpy(str,content["height"].c_str());
		int h=(int)atoi(str);
		return h;
	}
	bool isMax()
	{
		string bool_true("true");
		return bool_true==content["ismax"];
	}
	void clear()
	{
		content.clear();
		extensions.clear();
	}
	list<extension> extensions;
	int extensionNum;
} app;

class JSON_API Parser
{
public:
	Parser(const string&);
	~Parser();
	void setSn(const string&);								//a function to set the serial-number;
	void parse();											//a function to parse json.if you want to fetch the json from the internet again,just call setSn(if needed) and re-call this function
	string getJson()const;		
	list<extension> getExtensions()const;					//to get a list of extensions
	app getApp()const;										//to get a instance of struct App
private:
	string _json;											//the json
	string _sn;												//the serial-number
	app _a;													//a instance of struct App
};
}
#endif